import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yyh
 * @create 2020-09-18 7:54
 */
public class MyThread {
//    public static void main(String[] args) {
//        Phone phone = new Phone();
//
//
//
//        //锁的存在
//        new Thread(()->{
//            phone.sendSms();
//        },"A").start();
//
//        // 捕获
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        new Thread(()->{
//            phone.call();
//        },"B").start();
//
//
//        new Thread(()->{
//            phone.hello();
//        },"C").start();
//
//    }

    // CAS compareAndSet : 比较并交换！
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);
// 期望、更新
// public final boolean compareAndSet(int expect, int update)
// 如果我期望的值达到了，那么就更新，否则，就不更新, CAS 是CPU的并发原语！
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get());
        atomicInteger.getAndIncrement();
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get());

        long totalmemory = Runtime.getRuntime().totalMemory();
        long maxmemory = Runtime.getRuntime().maxMemory();
        System.out.println(totalmemory);
        System.out.println(maxmemory);
    }
}
class Phone{

    // synchronized 锁的对象是方法的调用者！、

    // 两个方法用的是同一个锁，谁先拿到谁执行！
    public synchronized void sendSms(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public synchronized void call(){
        System.out.println("打电话");
    }

    public void hello(){
        System.out.println("hello");
    }

}